{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SciPy\n",
    "<table align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"http://nbviewer.ipython.org/github/ShowMeAI-Hub/awesome-AI-cheatsheets/blob/main/Scipy/Scipy-cheatsheet-code.ipynb\"><img src=\"https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg\" />在nbviewer上查看notebook</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/ShowMeAI-Hub/awesome-AI-cheatsheets/blob/main/Scipy/Scipy-cheatsheet-code.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" />在Google Colab运行</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/ShowMeAI-Hub/awesome-AI-cheatsheets/tree/main/Scipy/Scipy-cheatsheet-code.ipynb\"><img src=\"https://badgen.net/badge/open/github/color=cyan?icon=github\" />在Github上查看源代码</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/ShowMeAI-Hub/awesome-AI-cheatsheets/Scipy/Scipy速查表.pdf\"><img src=\"https://badgen.net/badge/download/pdf/color=white?icon=github\"/>下载速查表</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 说明\n",
    "**notebook by [韩信子](https://github.com/HanXinzi-AI)@[ShowMeAI](https://github.com/ShowMeAI-Hub)**\n",
    "\n",
    "更多AI速查表资料请查看[速查表大全](https://github.com/ShowMeAI-Hub/awesome-AI-cheatsheets)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SciPy是基于NumPy创建的Python科学计算核心库，提供了众多数学算法与函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 与NumPy交互"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([(1+5j, 2j, 3j), (4j, 5j, 6j)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = np.array([[(1.5, 2, 3), (4, 5, 6)], [(3, 2, 1), (4, 5, 6)]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 索引技巧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 0, 0, 0, 0],\n",
       "        [1, 1, 1, 1, 1],\n",
       "        [2, 2, 2, 2, 2],\n",
       "        [3, 3, 3, 3, 3],\n",
       "        [4, 4, 4, 4, 4]],\n",
       "\n",
       "       [[0, 1, 2, 3, 4],\n",
       "        [0, 1, 2, 3, 4],\n",
       "        [0, 1, 2, 3, 4],\n",
       "        [0, 1, 2, 3, 4],\n",
       "        [0, 1, 2, 3, 4]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mgrid[0:5, 0:5]   #创建稠密栅格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0],\n",
       "        [1]]),\n",
       " array([[0, 1]])]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ogrid[0:2, 0:2]   #创建开放栅格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3.        ,  0.        ,  0.        ,  0.        ,  0.        ,\n",
       "        0.        , -1.        , -0.77777778, -0.55555556, -0.33333333,\n",
       "       -0.11111111,  0.11111111,  0.33333333,  0.55555556,  0.77777778,\n",
       "        1.        ])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.r_[3, [0]*5, -1:1:10j]   #按行纵向堆叠数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1],\n",
       "       [2, 2],\n",
       "       [3, 3]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.c_[a, a]   #按列横向堆叠数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 操控形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.+5.j, 0.+4.j],\n",
       "       [0.+2.j, 0.+5.j],\n",
       "       [0.+3.j, 0.+6.j]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(b)   #转置矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.+5.j, 0.+2.j, 0.+3.j, 0.+4.j, 0.+5.j, 0.+6.j])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.flatten()   #拉平数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1.5, 2. , 3. ],\n",
       "        [4. , 5. , 6. ],\n",
       "        [1.5, 2. , 3. ],\n",
       "        [4. , 5. , 6. ]],\n",
       "\n",
       "       [[3. , 2. , 1. ],\n",
       "        [4. , 5. , 6. ],\n",
       "        [3. , 2. , 1. ],\n",
       "        [4. , 5. , 6. ]]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((c, c))   #按列横向堆叠数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.+0.j, 2.+0.j, 3.+0.j],\n",
       "       [1.+5.j, 0.+2.j, 0.+3.j],\n",
       "       [0.+4.j, 0.+5.j, 0.+6.j]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a, b))   #按行纵向堆叠数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[[1.5, 2. , 3. ]],\n",
       " \n",
       "        [[3. , 2. , 1. ]]]),\n",
       " array([[[4., 5., 6.]],\n",
       " \n",
       "        [[4., 5., 6.]]])]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(c, 2)   #在索引2横向分割数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[[1.5, 2. , 3. ],\n",
       "         [4. , 5. , 6. ]]]),\n",
       " array([[[3., 2., 1.],\n",
       "         [4., 5., 6.]]])]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(c, 2)   #在索引2纵向分割数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多项式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "from numpy import poly1d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = poly1d([3, 4, 5])   #创建多项式对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矢量函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "def myfunc(a):\n",
    "    if a < 0:\n",
    "        return a*2\n",
    "    else:\n",
    "        return a/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<numpy.vectorize at 0x10e37be50>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vectorize(myfunc)   #矢量函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类型控制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1.5, 2. , 3. ],\n",
       "        [4. , 5. , 6. ]],\n",
       "\n",
       "       [[3. , 2. , 1. ],\n",
       "        [4. , 5. , 6. ]]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.real(c)   #返回数组元素的实部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0., 0., 0.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[0., 0., 0.],\n",
       "        [0., 0., 0.]]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.imag(c)   #返回数组元素的虚部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1.5, 2. , 3. ],\n",
       "        [4. , 5. , 6. ]],\n",
       "\n",
       "       [[3. , 2. , 1. ],\n",
       "        [4. , 5. , 6. ]]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.real_if_close(c, tol=1000)   #如果复数接近0，返回实部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(3.1415927, dtype=float32)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cast['f'](np.pi)   #将对象转化为数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[78.69006753, 90.        , 90.        ],\n",
       "       [90.        , 90.        , 90.        ]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.angle(b, deg=True)   #返回复数的辐角"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = np.linspace(0, np.pi, num=5)   #创建等差数组(样本数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "g [3:] += np.pi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  0.78539816,  1.57079633, -0.78539816,  0.        ])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unwrap(g)   #解包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.e+00, 1.e+05, 1.e+10])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(0, 10, 3)   #创建等差数组(对数刻度)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[3., 4., 6.],\n",
       "        [0., 0., 0.]],\n",
       "\n",
       "       [[6., 4., 2.],\n",
       "        [0., 0., 0.]]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.select([c<4], [c*2])   #根据条件返回数组列表的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 6.])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy import special\n",
    "special.factorial(a)   #因子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scipy\n",
    "scipy.special.comb(10, 3, exact=True)   #计算排列组合$$C_{10}^3$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.5,  0. ,  0.5])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy import misc\n",
    "misc.central_diff_weights(3)   #NP点中心导数的权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "misc.derivative(myfunc, 1.0)   #查找函数在某点的第n个导数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性代数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用linalg和sparse模块。注意scipy.linalg包含了numpy.linalg，并扩展了其功能。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    " from scipy import linalg, sparse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.matrix(np.random.random((2, 2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.asmatrix(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "C = np.mat(np.random.random((10, 5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "D = np.mat([[3, 4], [5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基础矩阵操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**逆矩阵**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-0.25327031,  2.58843168],\n",
       "        [ 1.20275974, -0.77494263]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.I   #求逆矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.25327031,  2.58843168],\n",
       "       [ 1.20275974, -0.77494263]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.inv(A)   #求逆矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.26566503, 0.41232885],\n",
       "        [0.88736347, 0.08682586]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.T   #矩阵转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.26566503, 0.41232885],\n",
       "        [0.88736347, 0.08682586]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.H   #共轭转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.35249089510058107"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.trace(A)   #计算对角线元素的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**范数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.017617637071628"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.norm(A)   #Frobeniu范数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.974189337644343"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.norm(A, 1)   #L1范数 (最大列汇总)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.1530285036832062"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.norm(A, np.inf)   #L范数 (最大列汇总)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**排名**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.matrix_rank(C)   #矩阵排名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**行列式**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.34281896670762335"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.det(A)   #行列式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**求解线性问题**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.25327031 +9.08737518j,  0.        +12.43561778j,\n",
       "         0.        +14.77077915j],\n",
       "       [ 1.20275974 +2.91402821j,  0.         -1.46919365j,\n",
       "         0.         -1.04137653j]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.solve(A, b)   #求解稠密矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "E = np.mat(a).T   #求解稠密矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1.00000000e+00, 3.29698799e-16],\n",
       "        [2.67591619e-15, 1.00000000e+00]]),\n",
       " array([], dtype=float64),\n",
       " 2,\n",
       " array([9.27110906, 0.21572392]))"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.lstsq(D, D)   #用最小二乘法求解线性代数方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**广义逆**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.01836107,  0.48839801, -0.77354577, -0.15300783,  0.52277402,\n",
       "         0.07791463,  0.27693506, -0.13188957,  0.19517763, -0.29426094],\n",
       "       [ 0.34857071, -0.12215497, -0.16260077,  0.17589514,  0.09894287,\n",
       "         0.04949279,  0.04332514, -0.7564116 ,  0.59867828,  0.19265664],\n",
       "       [ 0.63293288, -0.11895645,  0.15830958, -0.15509941, -0.22435947,\n",
       "        -0.30016429,  0.29375901,  0.63657948, -0.37477849, -0.31617618],\n",
       "       [-0.88711334, -0.39384048,  0.43868047, -0.14158081,  0.00791445,\n",
       "         0.71233746, -0.31595224,  0.5916117 ,  0.18805527,  0.23333057],\n",
       "       [ 0.12213004,  0.44716185,  0.29899879,  0.41017813, -0.03910791,\n",
       "        -0.38756352,  0.03331016, -0.31654379, -0.38983363,  0.19814132]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.pinv(C)   #计算矩阵的伪逆(最小二乘法求解器)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.01836107,  0.48839801, -0.77354577, -0.15300783,  0.52277402,\n",
       "         0.07791463,  0.27693506, -0.13188957,  0.19517763, -0.29426094],\n",
       "       [ 0.34857071, -0.12215497, -0.16260077,  0.17589514,  0.09894287,\n",
       "         0.04949279,  0.04332514, -0.7564116 ,  0.59867828,  0.19265664],\n",
       "       [ 0.63293288, -0.11895645,  0.15830958, -0.15509941, -0.22435947,\n",
       "        -0.30016429,  0.29375901,  0.63657948, -0.37477849, -0.31617618],\n",
       "       [-0.88711334, -0.39384048,  0.43868047, -0.14158081,  0.00791445,\n",
       "         0.71233746, -0.31595224,  0.5916117 ,  0.18805527,  0.23333057],\n",
       "       [ 0.12213004,  0.44716185,  0.29899879,  0.41017813, -0.03910791,\n",
       "        -0.38756352,  0.03331016, -0.31654379, -0.38983363,  0.19814132]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.pinv2(C)   #计算矩阵的伪逆(SVD)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建稀疏矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "F = np.eye(3, k=1)   #创建2X2单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "G = np.mat(np.identity(2))   #创建2X2单位矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "C[C > 0.5] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "H = sparse.csr_matrix(C)   #压缩稀疏行矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "I = sparse.csc_matrix(D)   #压缩稀疏列矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "J = sparse.dok_matrix(A)   #DOK矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3, 4],\n",
       "        [5, 6]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I.todense()   #将稀疏矩阵转为全矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sparse.isspmatrix_csc(A)   #单位稀疏矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 稀疏矩阵操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**逆矩阵**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<2x2 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 4 stored elements in Compressed Sparse Column format>"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scipy.sparse.linalg as linalg\n",
    "linalg.inv(I)   #求逆矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**范数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.273618495495704"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.norm(I)   #范数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**解决线性问题**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<2x2 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 3 stored elements in Compressed Sparse Column format>"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.spsolve(I, I)   #稀求解疏矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 稀疏矩阵函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<2x2 sparse matrix of type '<class 'numpy.float64'>'\n",
       "\twith 4 stored elements in Compressed Sparse Column format>"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sparse.linalg.expm(I)   #稀疏矩阵指数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**加法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[3.26566503, 4.88736347],\n",
       "        [5.41232885, 6.08682586]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.add(A, D)   #加法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**减法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-2.73433497, -3.11263653],\n",
       "        [-4.58767115, -5.91317414]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.subtract(A, D)   #减法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**除法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.08855501, 0.22184087],\n",
       "        [0.08246577, 0.01447098]])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.divide(A, D)   #除法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**乘法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.79699509, 3.54945389],\n",
       "        [2.06164426, 0.52095519]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(D, A)   #乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[5.23381246, 6.38684096],\n",
       "        [1.67111588, 2.1702706 ]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(A, D)   #点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.79699509]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vdot(A, D)   #向量点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[4.34644898, 6.65250599],\n",
       "        [1.58429002, 2.58259945]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.inner(A, D)   #内积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.79699509, 1.06266012, 1.32832515, 1.59399018],\n",
       "       [2.66209042, 3.54945389, 4.43681737, 5.32418084],\n",
       "       [1.23698656, 1.64931541, 2.06164426, 2.47397312],\n",
       "       [0.26047759, 0.34730346, 0.43412932, 0.52095519]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.outer(A, D)   #外积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(6.92904843)"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tensordot(A, D)   #张量点积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.79699509, 1.06266012, 2.66209042, 3.54945389],\n",
       "        [1.32832515, 1.59399018, 4.43681737, 5.32418084],\n",
       "        [1.23698656, 1.64931541, 0.26047759, 0.34730346],\n",
       "        [2.06164426, 2.47397312, 0.43412932, 0.52095519]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.kron(A, D)   #Kronecker积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function expm_multiply in module scipy.sparse.linalg._expm_multiply:\n",
      "\n",
      "expm_multiply(A, B, start=None, stop=None, num=None, endpoint=None)\n",
      "    Compute the action of the matrix exponential of A on B.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    A : transposable linear operator\n",
      "        The operator whose exponential is of interest.\n",
      "    B : ndarray\n",
      "        The matrix or vector to be multiplied by the matrix exponential of A.\n",
      "    start : scalar, optional\n",
      "        The starting time point of the sequence.\n",
      "    stop : scalar, optional\n",
      "        The end time point of the sequence, unless `endpoint` is set to False.\n",
      "        In that case, the sequence consists of all but the last of ``num + 1``\n",
      "        evenly spaced time points, so that `stop` is excluded.\n",
      "        Note that the step size changes when `endpoint` is False.\n",
      "    num : int, optional\n",
      "        Number of time points to use.\n",
      "    endpoint : bool, optional\n",
      "        If True, `stop` is the last time point.  Otherwise, it is not included.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    expm_A_B : ndarray\n",
      "         The result of the action :math:`e^{t_k A} B`.\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    The optional arguments defining the sequence of evenly spaced time points\n",
      "    are compatible with the arguments of `numpy.linspace`.\n",
      "    \n",
      "    The output ndarray shape is somewhat complicated so I explain it here.\n",
      "    The ndim of the output could be either 1, 2, or 3.\n",
      "    It would be 1 if you are computing the expm action on a single vector\n",
      "    at a single time point.\n",
      "    It would be 2 if you are computing the expm action on a vector\n",
      "    at multiple time points, or if you are computing the expm action\n",
      "    on a matrix at a single time point.\n",
      "    It would be 3 if you want the action on a matrix with multiple\n",
      "    columns at multiple time points.\n",
      "    If multiple time points are requested, expm_A_B[0] will always\n",
      "    be the action of the expm at the first time point,\n",
      "    regardless of whether the action is on a vector or a matrix.\n",
      "    \n",
      "    References\n",
      "    ----------\n",
      "    .. [1] Awad H. Al-Mohy and Nicholas J. Higham (2011)\n",
      "           \"Computing the Action of the Matrix Exponential,\n",
      "           with an Application to Exponential Integrators.\"\n",
      "           SIAM Journal on Scientific Computing,\n",
      "           33 (2). pp. 488-511. ISSN 1064-8275\n",
      "           http://eprints.ma.man.ac.uk/1591/\n",
      "    \n",
      "    .. [2] Nicholas J. Higham and Awad H. Al-Mohy (2010)\n",
      "           \"Computing Matrix Functions.\"\n",
      "           Acta Numerica,\n",
      "           19. 159-208. ISSN 0962-4929\n",
      "           http://eprints.ma.man.ac.uk/1451/\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> from scipy.sparse import csc_matrix\n",
      "    >>> from scipy.sparse.linalg import expm, expm_multiply\n",
      "    >>> A = csc_matrix([[1, 0], [0, 1]])\n",
      "    >>> A.todense()\n",
      "    matrix([[1, 0],\n",
      "            [0, 1]], dtype=int64)\n",
      "    >>> B = np.array([np.exp(-1.), np.exp(-2.)])\n",
      "    >>> B\n",
      "    array([ 0.36787944,  0.13533528])\n",
      "    >>> expm_multiply(A, B, start=1, stop=2, num=3, endpoint=True)\n",
      "    array([[ 1.        ,  0.36787944],\n",
      "           [ 1.64872127,  0.60653066],\n",
      "           [ 2.71828183,  1.        ]])\n",
      "    >>> expm(A).dot(B)                  # Verify 1st timestep\n",
      "    array([ 1.        ,  0.36787944])\n",
      "    >>> expm(1.5*A).dot(B)              # Verify 2nd timestep\n",
      "    array([ 1.64872127,  0.60653066])\n",
      "    >>> expm(2*A).dot(B)                # Verify 3rd timestep\n",
      "    array([ 2.71828183,  1.        ])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(linalg.expm_multiply)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**指数函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.5361597 , 1.12558146],\n",
       "       [0.52302098, 1.3093101 ]])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linalg.expm(A)   #矩阵指数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**对数函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.49189531+1.34108333j,  0.43049609-2.27957812j],\n",
       "       [ 0.20003749-1.05924557j, -0.57865746+1.80050933j]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.logm(A)   #矩阵对数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**三角函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.07153039,  0.17875259],\n",
       "       [ 0.22344073,  0.06253405]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.sinm(D)   #矩阵正弦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.30988067, -0.82893511],\n",
       "       [-1.03616888, -0.31182066]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.cosm(D)   #矩阵余弦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.3772929 , 1.06633638],\n",
       "       [0.49549172, 0.16238355]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.tanm(A)   #矩阵切线"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**双曲三角函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1716.2338286 , 2134.23383225],\n",
       "       [2667.79229031, 3316.90920278]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.sinhm(D)   #双曲矩阵正弦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1717.05255912, 2133.70712649],\n",
       "       [2667.13390811, 3317.33290398]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.coshm(D)   #双曲矩阵余弦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.20172133, 0.774065  ],\n",
       "       [0.35968275, 0.04571633]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.tanhm(A)   #双曲矩阵切线"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**矩阵符号函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1., 1.],\n",
       "        [1., 1.]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sign(A)   #矩阵符号函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**矩阵平方根**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.50865931+0.28161567j, 0.64400035-0.4786913j ],\n",
       "       [0.29924595-0.22243223j, 0.37886753+0.37809108j]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.sqrtm(A)   #矩阵平方根"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**任意函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.43646347, 0.31278754],\n",
       "       [0.14534217, 0.37342429]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.funm(A, lambda x: x*x)   #评估矩阵函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 矩阵分解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**特征值与特征向量**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [],
   "source": [
    "la, v = scipy.linalg.eig(A)   #求解方阵的普通或广义特征值问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "l1, l2 = la   #解包特征值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.8619086 , 0.50706367])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v[:, 0]   #第一个特征值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.78474171,  0.61982292])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v[:, 1]   #第二个特征值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.78770389+0.j, -0.43521299+0.j])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scipy.linalg.eigvals(A)   #解包特征值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**奇异值分解**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "U, s, Vh = scipy.linalg.svd(B)   #奇异值分解(SVD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "M, N = B.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sig = scipy.linalg.diagsvd(s, M, N)   #在SVD中构建Sigma矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**LU 分解**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "P, L, U = scipy.linalg.lu(C)   #LU分解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 解构稀疏矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "la, v = sparse.linalg.eigs(F, 1)   #特征值与特征向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-0.08884689,  0.18491492],\n",
       "        [-0.52298446,  0.19976149],\n",
       "        [ 0.01159765,  0.01748911],\n",
       "        [ 0.05954778,  0.35940384],\n",
       "        [ 0.        ,  0.        ],\n",
       "        [ 0.19981333,  0.65881768],\n",
       "        [ 0.        ,  0.        ],\n",
       "        [ 0.17972496,  0.35148866],\n",
       "        [ 0.08862015,  0.45789139],\n",
       "        [-0.79676413,  0.17080778]]),\n",
       " array([0.71124383, 1.10843563]),\n",
       " array([[ 0.28316372, -0.49874568, -0.07522717, -0.8050678 ,  0.13144487],\n",
       "        [ 0.66546825,  0.09519761,  0.64146554,  0.16883067,  0.32879719]]))"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sparse.linalg.svds(H, 2)   #奇异值分解(SVD)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调用帮助"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**help函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function diagsvd in module scipy.linalg.decomp_svd:\n",
      "\n",
      "diagsvd(s, M, N)\n",
      "    Construct the sigma matrix in SVD from singular values and size M, N.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    s : (M,) or (N,) array_like\n",
      "        Singular values\n",
      "    M : int\n",
      "        Size of the matrix whose singular values are `s`.\n",
      "    N : int\n",
      "        Size of the matrix whose singular values are `s`.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    S : (M, N) ndarray\n",
      "        The S-matrix in the singular value decomposition\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    svd : Singular value decomposition of a matrix\n",
      "    svdvals : Compute singular values of a matrix.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> from scipy.linalg import diagsvd\n",
      "    >>> vals = np.array([1, 2, 3])  # The array representing the computed svd\n",
      "    >>> diagsvd(vals, 3, 4)\n",
      "    array([[1, 0, 0, 0],\n",
      "           [0, 2, 0, 0],\n",
      "           [0, 0, 3, 0]])\n",
      "    >>> diagsvd(vals, 4, 3)\n",
      "    array([[1, 0, 0],\n",
      "           [0, 2, 0],\n",
      "           [0, 0, 3],\n",
      "           [0, 0, 0]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(scipy.linalg.diagsvd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " matrix()\n",
      "\n",
      "matrix(data, dtype=None, copy=True)\n",
      "\n",
      ".. note:: It is no longer recommended to use this class, even for linear\n",
      "          algebra. Instead use regular arrays. The class may be removed\n",
      "          in the future.\n",
      "\n",
      "Returns a matrix from an array-like object, or from a string of data.\n",
      "A matrix is a specialized 2-D array that retains its 2-D nature\n",
      "through operations.  It has certain special operators, such as ``*``\n",
      "(matrix multiplication) and ``**`` (matrix power).\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "data : array_like or string\n",
      "   If `data` is a string, it is interpreted as a matrix with commas\n",
      "   or spaces separating columns, and semicolons separating rows.\n",
      "dtype : data-type\n",
      "   Data-type of the output matrix.\n",
      "copy : bool\n",
      "   If `data` is already an `ndarray`, then this flag determines\n",
      "   whether the data is copied (the default), or whether a view is\n",
      "   constructed.\n",
      "\n",
      "See Also\n",
      "--------\n",
      "array\n",
      "\n",
      "Examples\n",
      "--------\n",
      ">>> a = np.matrix('1 2; 3 4')\n",
      ">>> a\n",
      "matrix([[1, 2],\n",
      "        [3, 4]])\n",
      "\n",
      ">>> np.matrix([[1, 2], [3, 4]])\n",
      "matrix([[1, 2],\n",
      "        [3, 4]])\n",
      "\n",
      "\n",
      "Methods:\n",
      "\n",
      "  all  --  Test whether all matrix elements along a given axis evaluate to True.\n",
      "  any  --  Test whether any array element along a given axis evaluates to True.\n",
      "  argmax  --  Indexes of the maximum values along an axis.\n",
      "  argmin  --  Indexes of the minimum values along an axis.\n",
      "  argpartition  --  a.argpartition(kth, axis=-1, kind='introselect', order=None)\n",
      "  argsort  --  a.argsort(axis=-1, kind=None, order=None)\n",
      "  astype  --  a.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)\n",
      "  byteswap  --  a.byteswap(inplace=False)\n",
      "  choose  --  a.choose(choices, out=None, mode='raise')\n",
      "  clip  --  a.clip(min=None, max=None, out=None, **kwargs)\n",
      "  compress  --  a.compress(condition, axis=None, out=None)\n",
      "  conj  --  a.conj()\n",
      "  conjugate  --  a.conjugate()\n",
      "  copy  --  a.copy(order='C')\n",
      "  cumprod  --  a.cumprod(axis=None, dtype=None, out=None)\n",
      "  cumsum  --  a.cumsum(axis=None, dtype=None, out=None)\n",
      "  diagonal  --  a.diagonal(offset=0, axis1=0, axis2=1)\n",
      "  dot  --  a.dot(b, out=None)\n",
      "  dump  --  a.dump(file)\n",
      "  dumps  --  a.dumps()\n",
      "  fill  --  a.fill(value)\n",
      "  flatten  --  Return a flattened copy of the matrix.\n",
      "  getA  --  Return `self` as an `ndarray` object.\n",
      "  getA1  --  Return `self` as a flattened `ndarray`.\n",
      "  getH  --  Returns the (complex) conjugate transpose of `self`.\n",
      "  getI  --  Returns the (multiplicative) inverse of invertible `self`.\n",
      "  getT  --  Returns the transpose of the matrix.\n",
      "  getfield  --  a.getfield(dtype, offset=0)\n",
      "  item  --  a.item(*args)\n",
      "  itemset  --  a.itemset(*args)\n",
      "  max  --  Return the maximum value along an axis.\n",
      "  mean  --  Returns the average of the matrix elements along the given axis.\n",
      "  min  --  Return the minimum value along an axis.\n",
      "  newbyteorder  --  arr.newbyteorder(new_order='S')\n",
      "  nonzero  --  a.nonzero()\n",
      "  partition  --  a.partition(kth, axis=-1, kind='introselect', order=None)\n",
      "  prod  --  Return the product of the array elements over the given axis.\n",
      "  ptp  --  Peak-to-peak (maximum - minimum) value along the given axis.\n",
      "  put  --  a.put(indices, values, mode='raise')\n",
      "  ravel  --  Return a flattened matrix.\n",
      "  repeat  --  a.repeat(repeats, axis=None)\n",
      "  reshape  --  a.reshape(shape, order='C')\n",
      "  resize  --  a.resize(new_shape, refcheck=True)\n",
      "  round  --  a.round(decimals=0, out=None)\n",
      "  searchsorted  --  a.searchsorted(v, side='left', sorter=None)\n",
      "  setfield  --  a.setfield(val, dtype, offset=0)\n",
      "  setflags  --  a.setflags(write=None, align=None, uic=None)\n",
      "  sort  --  a.sort(axis=-1, kind=None, order=None)\n",
      "  squeeze  --  Return a possibly reshaped matrix.\n",
      "  std  --  Return the standard deviation of the array elements along the given axis.\n",
      "  sum  --  Returns the sum of the matrix elements, along the given axis.\n",
      "  swapaxes  --  a.swapaxes(axis1, axis2)\n",
      "  take  --  a.take(indices, axis=None, out=None, mode='raise')\n",
      "  tobytes  --  a.tobytes(order='C')\n",
      "  tofile  --  a.tofile(fid, sep=\"\", format=\"%s\")\n",
      "  tolist  --  Return the matrix as a (possibly nested) list.\n",
      "  tostring  --  a.tostring(order='C')\n",
      "  trace  --  a.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)\n",
      "  transpose  --  a.transpose(*axes)\n",
      "  var  --  Returns the variance of the matrix elements, along the given axis.\n",
      "  view  --  a.view([dtype][, type])\n"
     ]
    }
   ],
   "source": [
    "np.info(np.matrix)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
